<?php
/**
 * 配送员类
 * @package  htdocs
 * @subpackage  module/base
 * @author   create by allen.qiang
 * @version 1.0
 */

class sysDistributionTechnician{

    /**
     *配送员查询
     */
    
    public function search( &$arg_array ) {

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();
        //配送员ID
        if (isset($arg_array['distribution_technician_id']) && quanli_is_int($arg_array['distribution_technician_id'])) {
            $sql .= ' AND distribution_technician_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['distribution_technician_id'];
        }
        //配送员编号
        if (isset($arg_array['distribution_technician_code']) && !empty($arg_array['distribution_technician_code'])) {
            $sql .= ' AND distribution_technician_code LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['distribution_technician_code']) . '%';
        }
        //配送员姓名
        if (isset($arg_array['distribution_technician_name']) && !empty($arg_array['distribution_technician_name'])) {
            $sql .= ' AND distribution_technician_name = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['distribution_technician_name']);
        }
        //手机号码
        if (isset($arg_array['mobile']) && is_numeric($arg_array['mobile'])) {
            $sql .= ' AND mobile = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['mobile'];
        }
        //配送站ID
        if (isset($arg_array['distribution_id']) && is_numeric($arg_array['distribution_id'])) {
            $sql .= ' AND distribution_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['distribution_id'];
        }
        //配送站名称
        if (isset($arg_array['distribution_name']) && is_numeric($arg_array['distribution_name'])) {
            $sql .= ' AND distribution_name = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['distribution_name'];
        }

        //配送员密码
        if (isset($arg_array['distribution_technician_pwd']) && is_numeric($arg_array['distribution_technician_pwd'])) {
            $sql .= ' AND distribution_technician_pwd = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['distribution_technician_pwd'];
        }
        //音频文件
        if (isset($arg_array['audio']) && is_numeric($arg_array['audio'])) {
            $sql .= ' AND audio = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['audio'];
        }
        //使用状态
        if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'], array(0, 1))) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        //绑定wx_openid
        if (isset($arg_array['wx_openid']) && $arg_array['wx_openid']!="") {
            $sql .= ' AND wx_openid = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['wx_openid'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_distribution_technician ' . $sql;
                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM sys_distribution_technician ' . $sql;

            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'creator'     	=> $item->creator,
                    'createTime'  	=> $item->createTime,
                    'distribution_technician_id'     => $item->distribution_technician_id,
                    'distribution_technician_code'   => $item->distribution_technician_code,
                    'distribution_technician_name'   => $item->distribution_technician_name,
                    'distribution_technician_pwd'   => $item->distribution_technician_pwd,
                    'mobile'    	=> $item->mobile,
                    'distribution_id'  => $item->distribution_id,
                    'distribution_name'    	=> $item->distribution_name,
                    'audioShow'       	=> G_IMAGE_URL . $item->audio,
                    'audio'       	=> $item->audio,
                    'useFlg'       	=> $item->useFlg
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
    }

    /**
     * 添加、修改配送员
     *
     */
    public function editDistribution ( $arg_array ) {

        global $G_SHOP;

        quanli_include('module/base/common/quanliFile.class.php');

        quanli_include('module/base/system/sysDistribution.class.php');

        $sysDistribution = new sysDistribution();

        $quanliFile = new quanliFile();

        //检查参数
        if (!isset( $arg_array['distribution_technician_code'],$arg_array['distribution_technician_name'],$arg_array['mobile'],$arg_array['distribution_id']) && !is_numeric($arg_array['distribution_technician_id'])){
            return array('message'=>'参数错误','code'=> 400);
        }

        if (empty($arg_array['distribution_technician_code']))

            return array( 'code' => 400, 'message' => '编号不能为空' );

        if (empty($arg_array['distribution_technician_name']))

            return array( 'code' => 400, 'message' => '调剂师姓名不能为空' );

        if (empty($arg_array['mobile']))

            return array( 'code' => 400, 'message' => '手机号码不能为空' );

//        if ($arg_array['distribution_id'] <= 0)
//            return array( 'code' => 400, 'message' => '请选择配送站' );


        $creator                          = $arg_array['creator'];
        $distribution_technician_id       = $arg_array['distribution_technician_id'] ;
        $distribution_technician_code     = $arg_array['distribution_technician_code'] ;
        $distribution_technician_name	  = $arg_array['distribution_technician_name'] ;
        $mobile                           = $arg_array['mobile'] ;
        //$distribution_technician_pwd      = $arg_array['distribution_technician_pwd'] ;
        //$distribution_id                  = $arg_array['distribution_id'] ;
        $audio                            = $arg_array['audio'] ;

        try{

            //生成新的音频文件地址
//            if ($audio) {
//                $new_file_address = $quanliFile->basename_db( $audio );
//            }

            // 查询配送站信息
//            $search_args_array = array(
//                'distribution_id' => $distribution_id
//            );
//            $distribution_array = $sysDistribution->search( $search_args_array );

            //验证手机号
            $check_sql = "SELECT COUNT(*) AS item_count FROM sys_distribution_technician WHERE mobile = '".$mobile."'";
            if($distribution_technician_id) $check_sql.= 'AND distribution_technician_id != '.$distribution_technician_id;

            $item_array = &$G_SHOP->DBCA->stmt_query($check_sql);
            if ($item_array && $item_array[0]->item_count)
                return array( 'code' => 400, 'message' => '手机号已存在' );


            $distribution_technician = new entity('sys_distribution_technician') ;
            if ($distribution_technician_id == 0) {
                $distribution_technician->creator = $creator;
                $distribution_technician->useFlg = 1;
                $distribution_technician->distribution_technician_id = generate_seqno('seqno_distribution_technician');
            } else {
                $distribution_technician->distribution_technician_id = $distribution_technician_id;
                if (!$G_SHOP->DBCA->fetch($distribution_technician)) {
                    throw new Exception('调剂师信息没有找到', 404);
                }
            }

            //保存配送员信息
            $distribution_technician->distribution_technician_code = $distribution_technician_code;
            $distribution_technician->distribution_technician_name = $distribution_technician_name;
            $distribution_technician->mobile = $mobile;
            //$distribution_technician->distribution_technician_pwd = $distribution_technician_pwd;
            //$distribution_technician->distribution_id = $distribution_id;
            
//            if ($audio) {
//                $distribution_technician->audio = $new_file_address;
//            }

            //$distribution_technician->distribution_name = $distribution_array[0]['distribution_name'];

            $G_SHOP->DBCA->save( $distribution_technician ) ;

            if ($distribution_technician_id > 0) {
                return array( 'code' => 200, 'message' => '调剂师修改成功' );
            } else {
                return array( 'code' => 200, 'message' => '调剂师保存成功' );
            }

        }catch (Exception $e) {
            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }
    }

    /**
     * 配送员启用/禁用
     * @param $arg_array
     * @return array
     */
    public function changeDistributionTechnicianUseflg ( $arg_array ) {

        global $G_SHOP;

        if (!isset($arg_array['distribution_technician_id'], $arg_array['flg']) || !quanli_is_int($arg_array['distribution_technician_id']) || !in_array($arg_array['flg'], array(0,1)))
            return array( 'code' => 400, 'message' => '参数错误' );

        try {

            //查询配送员
            $distributionTechnician_obj = new entity('sys_distribution_technician');
            $distributionTechnician_obj->distribution_technician_id = $arg_array['distribution_technician_id'];
            if (!$G_SHOP->DBCA->fetch($distributionTechnician_obj))
                return array( 'code' => 404, 'message' => '调剂师信息没有找到' );

            if ($arg_array['flg']) {

                if ($distributionTechnician_obj->useFlg == 0) {
                    return array( 'code' => 406, 'message' => '调剂师已禁用' );
                }
                $distributionTechnician_obj->useFlg = 0;

            } else {

                if ($distributionTechnician_obj->useFlg == 1) {
                    return array( 'code' => 406, 'message' => '调剂师已启用' );
                }
                $distributionTechnician_obj->useFlg = 1;

            }
            $G_SHOP->DBCA->save($distributionTechnician_obj);

            if ($arg_array['flg'])
                return array( 'code' => 200, 'message' => '调剂师禁用成功' );
            else
                return array( 'code' => 200, 'message' => '调剂师启用成功' );

        } catch (Exception $e) {
            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }

    }

    /**
     * 删除配送员
     * @param array $pay_array 配送员ID数组
     * @return array
     */
    public function deleteDistributionTechnician ( $pay_array ) {

        global $G_SHOP;

        if (!isset($pay_array) || !is_array($pay_array) || empty($pay_array)) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        try {

            $G_SHOP->DBCA->beginTransaction();

            foreach($pay_array as &$distribution_technician_id) {



                //查询配送员方式
                $pay_obj = new entity('sys_distribution_technician');
                $pay_obj->distribution_technician_id = $distribution_technician_id;
                if (!$G_SHOP->DBCA->fetch($pay_obj)) {
                    throw new Exception('调剂师信息没有找到', 404);
                }



                //删除配送员信息
                $G_SHOP->DBCA->delete($pay_obj);

            }

            $G_SHOP->DBCA->commit();

            return array( 'code' => 200, 'message' => '删除调剂师成功' );

        } catch ( Exception $e ) {

            $G_SHOP->DBCA->rollBack();

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }

    //绑定微信openId
    public function bindOpenId ( $id, $openId )
    {
        global $G_SHOP;
        //查询配送员
        $distributionTechnician_obj = new entity('sys_distribution_technician');
        $distributionTechnician_obj->distribution_technician_id = $id;
        if (!$G_SHOP->DBCA->fetch($distributionTechnician_obj))
            return array( 'code' => 404, 'message' => '调剂师信息没有找到' );

        $distributionTechnician_obj->wx_openid = $openId;
        $G_SHOP->DBCA->save($distributionTechnician_obj);
        return array( 'code' => 200, 'message' => '' );
    }

}
